package com.opmsys.controller;

import com.opmsys.pojo.dto.PumpingInfoDTO;
import com.opmsys.pojo.dto.PumpingPageDTO;
import com.opmsys.pojo.entity.Pumping;
import com.opmsys.pojo.entity.Record;
import com.opmsys.result.PageResult;
import com.opmsys.result.Result;
import com.opmsys.service.PumpingService;
import com.opmsys.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名称PumpingController
 * 描述
 *
 * @version 1.0
 * @author:冉启呈2021015445
 * @datatime:2024-06-17 22:27
 */
@RestController
@RequestMapping("/pumping")
public class PumpingController {
    @Autowired
    public PumpingService pumpingService;

    @Autowired
    public RecordService recordService;


    /**
     * 抽油机新增
     * @param pumping
     * @return
     */
    @PostMapping
    public Result addPumping(Pumping pumping) {
        pumpingService.insertPumping(pumping);
        return Result.success();
    }


    /**
     * 批量删除抽油机
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deletePumpingBatch(@RequestParam(required = false) List<Integer> ids) {
        for (Integer id : ids)
            pumpingService.deletePumping(id);
        return Result.success();
    }


    /**
     * 更新抽油机
     * @param pumping
     * @return
     */
    @PutMapping
    public Result updatePumping(Pumping pumping) {
        pumpingService.updatePumping(pumping);
        return Result.success();
    }


    /**
     * 模糊查询抽油机名称
     * @param pumpingPageDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> searchPumpings(PumpingPageDTO pumpingPageDTO) {
        PageResult result=new PageResult();
        List<Pumping> pumpings1=pumpingService.searchPumpings(pumpingPageDTO.getCharacteristic());
        int num=pumpings1.size();
        result.setTotal(num);
        List<Pumping> pumpings2=pumpingService.pagePumpings(pumpingPageDTO);
        result.setRecords(pumpings2);
        return Result.success(result);
    }

    /**
     * 抽油机详情查询
     * @param id
     * @return
     */
    @GetMapping("/unitdetails")
    public Result<PumpingInfoDTO> pumpingInfo(Integer id){
        Pumping pumping = pumpingService.selectById(id);
        List<Record> list = recordService.selectByPumpingId(id);
        PumpingInfoDTO pumpingInfoDTO = new PumpingInfoDTO(pumping,list);
        return Result.success(pumpingInfoDTO);
    }
}
